package org.jdemo.tools.bookmark;

import java.util.ArrayList;
import java.util.List;

/**
 * Bookmark
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 2014-3-10
 * @since V1.0
 */
public class Bookmark {
    private Integer level;
    private String title;
    private Integer page;

    private Bookmark parent;
    private List<Bookmark> children;

    public Bookmark() {
    }

    public Bookmark(Integer level) {
        this.level = level;
    }

    public Bookmark(Integer level, String title) {
        this.level = level;
        this.title = title;
    }

    public Bookmark(Integer level, String title, Integer page) {
        this.level = level;
        this.title = title;
        this.page = page;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer id) {
        this.level = level;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Bookmark getParent() {
        return parent;
    }

    public void setParent(Bookmark parent) {
        this.parent = parent;
    }

    public List<Bookmark> getChildren() {
        return children;
    }

    public void setChildren(List<Bookmark> children) {
        this.children = children;
    }

    public void addChild(Bookmark child) {
        if (this.children == null) {
            this.children = new ArrayList<Bookmark>();
        }
        this.children.add(child);
    }

    @Override
    public String toString() {
        String s = "";
        if (level > 1) {
            for (int i = level; i > 1; i--) {
                s += "  ";
            }
        }

        s += title;
        if (this.children != null && this.children.size() > 0) {
            for (Bookmark child : this.children) {
                s += "\n" + child.toString();
            }
        }
        return s;
    }

    public String toXML(int addPageCount) {
        String indent = "";
        for (int i = level; i > 0; i--) {
            indent += "  ";
        }

        String xml = indent + "<Title Open=\"false\" Action=\"GoTo\" Page=\"";
        Integer page = 0;
        Bookmark pageBookmark = this;
        while (pageBookmark != null) {
            if (pageBookmark.page != null) {
                page = pageBookmark.page;
            } else if (pageBookmark.parent != null) {
                for(Bookmark sibling : pageBookmark.parent.children) {
                    if (sibling.page != null) {
                        page = sibling.page;
                        break;
                    }
                }
            }
            if (page > 0) {
                break;
            }
            pageBookmark = pageBookmark.parent;
        }
        page += addPageCount;
        xml += page + " XYZ 0 700 0";
        xml += "\" >" + this.title;

        if (this.children != null && this.children.size() > 0) {
            for (Bookmark child : this.children) {
                xml += "\n" + child.toXML(addPageCount);
            }
            xml += "\n" + indent + "</Title >";
        } else {
            xml += "</Title>";
        }

        return xml;
    }

    public Bookmark getNextSibling() {
        if (this.parent != null) {
            for (int i = 0; i < this.parent.children.size(); i++) {
                Bookmark sibling = this.parent.children.get(i);
                if (sibling == this && i < this.parent.children.size() - 1) {
                    return this.parent.children.get(i + 1);
                }
            }
        }
        return null;
    }

    public Bookmark getPreSibling() {
        if (this.parent != null) {
            for (int i = 0; i < this.parent.children.size(); i++) {
                Bookmark sibling = this.parent.children.get(i);
                if (sibling == this && i > 0) {
                    return this.parent.children.get(i - 1);
                }
            }
        }
        return null;
    }
}
